﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.DataAccess;

namespace University.Workshop.Business
{
    public class ManagerJournalEntries
    {

        public static ICollection<Model.JournalEntry> SearchIncomeByDates(DateTime? initialDate, DateTime? endDate, ref decimal totalMoney)
        {
            if (initialDate == null)
                initialDate = DateTime.MinValue;
            if (endDate == null)
                endDate = DateTime.MaxValue;

            ICollection<Model.WaterCollectControl> watercollectControlList = DataAccessWaterCollectControl.GetBetweenDates(initialDate.Value, endDate.Value);
            ICollection<Model.OtherCollectControl> collectOtherControlList = DataAccessOtherCollectControl.GetBetweenDates(initialDate.Value, endDate.Value);
            ICollection<Model.FineCollectControl> fineControlList = DataAccessFineCollectControl.GetBetweenDates(initialDate.Value, endDate.Value);

            List<Model.JournalEntry> journalEntryList = new List<Model.JournalEntry>();
            //armar lista
            foreach (Model.CollectControl collectControl in watercollectControlList)
            {
                totalMoney += collectControl.PaymentAmount;
                Model.JournalEntry journal = new Model.JournalEntry() {
                                                                        //journalId = collectControl.CollectId,
                                                                        RecordDate = collectControl.CollectPayDate.Value,
                                                                        Amount = collectControl.PaymentAmount,
                                                                        Detail = "",
                                                                        TypeOfCharge = "Cobro de agua",
                                                                        TableCharge = (int)IncomeTable.CollectControl};
                journalEntryList.Add(journal);
            }
            foreach (Model.OtherCollectControl otherControl in collectOtherControlList)
            {
                totalMoney += otherControl.PaymentAmount;
                Model.JournalEntry journal = new Model.JournalEntry(){
                                                                        //journalId = otherControl.CollectId,
                                                                        RecordDate = otherControl.CollectPayDate.Value,
                                                                        Amount = otherControl.PaymentAmount,
                                                                        Detail = otherControl.ControlDetail,
                                                                        TypeOfCharge = ((OtherChargeType)otherControl.CollectType).ToString(),
                                                                        TableCharge = (int)IncomeTable.CollectOtherControl};
                journalEntryList.Add(journal);
            }

            foreach (Model.FineCollectControl fine in fineControlList)
            {
                totalMoney += fine.PaymentAmount;
                Model.JournalEntry journal = new Model.JournalEntry(){
                                                                        //journalId = fine.CollectId,
                                                                        RecordDate = fine.CollectPayDate.Value,
                                                                        Amount = fine.PaymentAmount,
                                                                        Detail = "",
                                                                        TypeOfCharge = ((FineType)fine.CollectType).ToString(),
                                                                        TableCharge = (int)IncomeTable.FineControl};
                journalEntryList.Add(journal);

            }

            return (from i in journalEntryList
                    orderby i.RecordDate ascending
                    select i).ToList();
        }

        public static ICollection<Model.JournalEntry> SearchOutflowByDates(DateTime? initialDate, DateTime? endDate, ref decimal totalMoney)
        {
            List<Model.JournalEntry> journalEntryList = new List<Model.JournalEntry>();
            if (initialDate == null)
                initialDate = DateTime.MinValue;
            if (endDate == null)
                endDate = DateTime.MaxValue;
            ICollection<Model.Expense> expenseList = DataAccessExpense.GetExpenseBetweenDates(initialDate.Value, endDate.Value);
            //gastos de compra de agua
            foreach (Model.Expense expense in expenseList)
            {
                totalMoney += expense.Price;
                Model.JournalEntry journalEntry = new Model.JournalEntry() { RecordDate = expense.DateRegistry, 
                                                                                Amount = expense.Price, 
                                                                                Detail = expense.Description, 
                                                                                TypeOfCharge = ((ExpenseType)expense.Item).ToString() };
                journalEntryList.Add(journalEntry);
            }
            if (journalEntryList.Count == 0)
            {
                throw new Exception("No se encontraron resultados de la busqueda...");
            }
            return (from i in journalEntryList
                    orderby i.RecordDate ascending
                    select i).ToList();
        }
    }
}
